package com.eshop.modules.canvas.service.impl;

import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.eshop.modules.canvas.service.dto.StoreCanvasDto;
import com.eshop.domain.PageResult;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.canvas.service.dto.StoreCanvasQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.canvas.service.StoreCanvasService;
import com.eshop.modules.canvas.domain.StoreCanvas;
import com.eshop.modules.canvas.service.mapper.StoreCanvasMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class StoreCanvasServiceImpl extends BaseServiceImpl<StoreCanvasMapper, StoreCanvas> implements StoreCanvasService
{
    private final IGenerator generator;
    
    public PageResult<StoreCanvasDto> queryAll(final StoreCanvasQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<StoreCanvas> page = (PageInfo<StoreCanvas>)new PageInfo((List)this.queryAll(criteria));
        return (PageResult<StoreCanvasDto>)this.generator.convertPageInfo((PageInfo)page, (Class)StoreCanvasDto.class);
    }
    
    public List<StoreCanvas> queryAll(final StoreCanvasQueryCriteria criteria) {
        return (List<StoreCanvas>)(this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate((Object)StoreCanvas.class, (Object)criteria));
    }
    
    public void download(final List<StoreCanvasDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final StoreCanvasDto storeCanvas : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("终端 1-小程序 2-H5 3-APP 4-PC", storeCanvas.getTerminal());
            map.put("画布json数据", storeCanvas.getJson());
            map.put("类型 1-系统画布 2-自定义页面 3-商家店铺装修", storeCanvas.getType());
            map.put("名称", storeCanvas.getName());
            map.put("店铺id，当type=3的时候，值为具体的店铺id，其它情况为0", storeCanvas.getShopId());
            map.put("创建时间", storeCanvas.getCreateTime());
            map.put("修改时间", storeCanvas.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public StoreCanvasServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
